import _ from 'lodash';
import { GenerateUUID } from '@/utils';

export const WrapRoot = (formObj: any, childIds: string[]) => {
  if (_.isEmpty(formObj) || !_.isObject(formObj)) {
    console.error('error params, formObj is not expected to be empty');
    return null;
  }
  formObj = { ...formObj };
  const uid = 'root_' + GenerateUUID();
  formObj[uid] = {
    childs: childIds,
    data: { props: {} },
    gaeaKey: 'gaea-container-helper',
    parentInstanceKey: null
  };
  return formObj;
};

export const findTemplateChilds = (
  result: any,
  instances: any,
  childIds: string[]
) => {
  childIds?.forEach((id: string) => {
    result[id] = instances.get(id);
    if (result[id] && result[id].childs && result[id].childs.length) {
      findTemplateChilds(result, instances, result[id].childs);
    }
  });
  return result;
};
